﻿// Decompiled with JetBrains decompiler
// Type: Netick.NativeIdStack
// Assembly: Netick, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 6726ECA1-C773-4CF7-8952-E81B30D93B52
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.dll
// XML documentation location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Runtime\Netick.xml

using System;

#nullable disable
namespace Netick;

internal struct NativeIdStack
{
  public unsafe int* Ptr;
  public int BufferSize;

  public int Count { get; private set; }

  public unsafe bool IsValid => (IntPtr) this.Ptr != IntPtr.Zero;

  public void InitIds()
  {
    for (int index = 0; index < this.BufferSize; ++index)
      this.Push(index);
  }

  public unsafe void ExpandCapacityAndInitNewIds(IAllocator allocator)
  {
    int bufferSize = this.BufferSize;
    NativeUtils.ResizeBuffer(allocator, this.BufferSize * 2, 4, ref this.BufferSize, ref this.Ptr);
    for (int index = bufferSize; index < this.BufferSize; ++index)
      this.Push(index);
  }

  public unsafe int Pop()
  {
    if (this.Count == 0)
      throw new InvalidOperationException();
    return this.Ptr[--this.Count];
  }

  public unsafe int Push(int index) => this.Ptr[this.Count++] = index;

  public unsafe void Allocate(IAllocator allocator, int initialCapacity)
  {
    NativeUtils.Allocate(allocator, true, initialCapacity, 4, ref this.BufferSize, ref this.Ptr);
  }

  public unsafe void Deallocate(IAllocator allocator)
  {
    NativeUtils.Deallocate(allocator, ref this.Ptr);
  }
}
